package com.issue.system.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.issue.common.core.domain.entity.SysUser;
import com.issue.system.domain.AChildInfo;
import com.issue.system.mapper.AChildInfoMapper;
import com.issue.system.mapper.ChildCourseMapper;
import com.issue.system.mapper.SysUserMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.issue.common.annotation.Log;
import com.issue.common.core.controller.BaseController;
import com.issue.common.core.domain.AjaxResult;
import com.issue.common.enums.BusinessType;
import com.issue.system.domain.ChildCourse;
import com.issue.system.service.IChildCourseService;
import com.issue.common.utils.poi.ExcelUtil;
import com.issue.common.core.page.TableDataInfo;

/**
 * CourseController
 * 
 * @author system
 * @date 2024-10-20
 */
@RestController
@RequestMapping("/system/course")
public class ChildCourseController extends BaseController
{
    @Autowired
    private IChildCourseService childCourseService;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private AChildInfoMapper childInfoMapper;

    /**
     * 查询Course列表
     */
    @PreAuthorize("@ss.hasPermi('system:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChildCourse childCourse)
    {
        startPage();
        List<ChildCourse> list = childCourseService.selectChildCourseList(childCourse);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:course:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(ChildCourse childCourse)
    {
        List<ChildCourse> list = childCourseService.selectChildCourseList(childCourse);
        return getDataTable(list);
    }

    /**
     * 导出Course列表
     */
    @PreAuthorize("@ss.hasPermi('system:course:export')")
    @Log(title = "Course", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChildCourse childCourse)
    {
        List<ChildCourse> list = childCourseService.selectChildCourseList(childCourse);
        ExcelUtil<ChildCourse> util = new ExcelUtil<ChildCourse>(ChildCourse.class);
        util.exportExcel(response, list, "Course数据");
    }

    /**
     * 获取Course详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:course:query')")
    @GetMapping(value = "/{courseId}")
    public AjaxResult getInfo(@PathVariable("courseId") Long courseId)
    {
        return success(childCourseService.selectChildCourseByCourseId(courseId));
    }

    /**
     * 新增Course
     */
    @PreAuthorize("@ss.hasPermi('system:course:add')")
    @Log(title = "Course", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChildCourse childCourse)
    {
        Long teacherId = childCourse.getTeacherId();
        Long childId = childCourse.getChildId();

        if (teacherId == null || childId == null) {
            return AjaxResult.error("Error Message");
        }

        SysUser teacher = sysUserMapper.selectUserById(teacherId);
        if (teacher == null){
            return AjaxResult.error("Error Teacher");
        }
        childCourse.setTeacherName(teacher.getUserName());

        AChildInfo childInfo = childInfoMapper.selectAChildInfoByChildId(childId);
        if (childInfo == null){
            return AjaxResult.error("Error Child");
        }
        childCourse.setChildCardNo(childInfo.getChildCardNo());
        childCourse.setChildName(childInfo.getChildName());

        return toAjax(childCourseService.insertChildCourse(childCourse));
    }

    /**
     * 修改Course
     */
    @PreAuthorize("@ss.hasPermi('system:course:edit')")
    @Log(title = "Course", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChildCourse childCourse)
    {

        Long teacherId = childCourse.getTeacherId();
        Long childId = childCourse.getChildId();

        if (teacherId != null) {
            SysUser teacher = sysUserMapper.selectUserById(teacherId);
            if (teacher != null){
                childCourse.setTeacherName(teacher.getUserName());
            }
        }

        if (childId != null) {
            AChildInfo childInfo = childInfoMapper.selectAChildInfoByChildId(childId);
            if (childInfo != null){
                childCourse.setChildCardNo(childInfo.getChildCardNo());
                childCourse.setChildName(childInfo.getChildName());
            }
        }

        return toAjax(childCourseService.updateChildCourse(childCourse));
    }

    /**
     * 删除Course
     */
    @PreAuthorize("@ss.hasPermi('system:course:remove')")
    @Log(title = "Course", businessType = BusinessType.DELETE)
	@DeleteMapping("/{courseIds}")
    public AjaxResult remove(@PathVariable Long[] courseIds)
    {
        return toAjax(childCourseService.deleteChildCourseByCourseIds(courseIds));
    }
}
